﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CompositionHelper.cs" company="None">
//   <Author>Adrian von Allmen, 2013</Author>
// </copyright>
// <summary>
//   A Helper Class which contains all composition methods
//   Currently not working - Have to find a approach. Possible solution could be: http://www.createdbyx.com/createdbyx/post/2013/04/03/Code-Snippets-13-–-MEF-Helpers.aspx
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CrumbleLibrary
{
    using System;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.IO;
    using System.Reflection;
    using System.Text;

    /// <summary>
    /// A Helper Class which contains all composition methods
    /// </summary>
    public class CompositionHelper
    {
        /// <summary>
        /// Load Composition Parts by defined paths
        /// </summary>
        /// <param name="partReference">
        /// The part Reference.
        /// </param>
        public void LoadComposition(params object[] partReference)
        {
            var catalogPath = new StringBuilder();

            try
            {
                // Read Composition for Report, Filter and future plugins
                var reportCatalog = new AggregateCatalog();
                string codeBase = Assembly.GetExecutingAssembly().CodeBase;
                var uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                string assemblyDirectory = Path.GetDirectoryName(path);
                if (assemblyDirectory != null)
                {
                    catalogPath.AppendLine(assemblyDirectory);
                    reportCatalog.Catalogs.Add(new DirectoryCatalog(assemblyDirectory));
                }

                // ToDo: Injection of personal Paths
#if DEBUG
                catalogPath.AppendLine(@"E:\Projekte\Codeplex TFS\crumble\Reports\Report_Sample\bin\Debug\");
                reportCatalog.Catalogs.Add(new DirectoryCatalog(@"E:\Projekte\Codeplex TFS\crumble\Reports\Report_Sample\bin\Debug\"));
#endif
                var reportWorker = new CompositionContainer(reportCatalog);
                reportWorker.ComposeParts(partReference);
            }
            catch (ReflectionTypeLoadException typeLException)
            {
                var loaderMessages = new StringBuilder();
                loaderMessages.AppendLine("While trying to load composable parts the follwing loader exceptions were found: ");
                foreach (var loaderException in typeLException.LoaderExceptions)
                {
                    loaderMessages.AppendLine(loaderException.Message);
                }

                loaderMessages.AppendLine("Directory Involved");
                loaderMessages.AppendLine(catalogPath.ToString());

                throw new Exception(loaderMessages.ToString(), typeLException);
            }
        }
    }
}
